package com.example.springbook.service;
import com.example.springbook.enums.BookStatus;
import com.example.springbook.mapper.BookInfoMapper;
import com.example.springbook.model.BookInfo;
import com.example.springbook.model.PageRequest;
import com.example.springbook.model.PageResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }
    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        //1. 查询记录的总数
        //1. 查询记录的总数
        Integer count = bookInfoMapper.count();
        //2. 查询当前页的数据
        List<BookInfo> bookInfos = bookInfoMapper.queryListBypage(pageRequest);
        for (BookInfo bookInfo:bookInfos){
            //根据状态, 设置描述
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getName());
        }
        return new PageResult<>(bookInfos,count,pageRequest);

    }
    PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();
        List<BookInfo> bookInfos = bookInfoMapper.queryBookListByPage(pageRequest);
        for (BookInfo bookInfo:bookInfos) {
            if (bookInfo.getStatus() == 0) {
                bookInfo.setStatusCN("⽆效");
            } else if (bookInfo.getStatus() == 1) {
                bookInfo.setStatusCN("可借阅");
            } else {
                bookInfo.setStatusCN("不可借阅");
            }
        }
        return new PageResult<>(bookInfos,count,pageRequest);
    }
    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }
public Integer insertBook(BookInfo bookInfo){
        return bookInfoMapper.insertBook(bookInfo);
}
    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(@Param("ids") List<Integer> ids){
        return bookInfoMapper.batchDelete(ids);
    }

}

